package com.zhangshihao.employee.controller;

import com.zhangshihao.employee.domain.Employee;
import com.zhangshihao.employee.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/member")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/register")
    public ResponseEntity<Map<String, Object>> register(@RequestBody Employee employee) {
        employeeService.register(employee);
        return ResponseEntity.ok(Collections.singletonMap("code", "000000"));
    }

    @GetMapping
    public ResponseEntity<Map<String, Object>> listAll() {
        List<Employee> employees = employeeService.listAll();
        Map<String, Object> response = new HashMap<>();
        response.put("code", "000000");
        response.put("data", employees);
        return ResponseEntity.ok(response);
    }

    @PutMapping
    public ResponseEntity<Map<String, Object>> update(@RequestBody Employee employee) {
        employeeService.update(employee);
        return ResponseEntity.ok(Collections.singletonMap("code", "000000"));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> delete(@PathVariable Integer id) {
        employeeService.delete(id);
        return ResponseEntity.ok(Collections.singletonMap("code", "000000"));
    }
}